var md5 = require('md5-node');
var post = require('../post');
var uuid = require('node-uuid');
function getSign(client_id,timestamp,client_secret){
    let sign = client_id+timestamp+client_secret;
    return md5(sign);
}

function getTimestamp(){
    return Math.floor(Date.now() / 1000);
}
function init(client_id,access_token,user_id,machine_code,msign,client_secret){

    this.client_id = client_id;
    this.access_token = access_token;
    this.user_id = user_id;
    this.machine_code = machine_code;
    this.msign = msign;
    this.client_secret = client_secret;
    this.fun ={

        getAccessToken:async ()=>{
            this.timestamp = getTimestamp();
            this.sign = getSign(this.client_id,this.timestamp,this.client_secret)
            try {
                let body = await post('https://open-api.10ss.net/oauth/oauth',{
                    client_id: this.client_id,
                    grant_type: 'client_credentials',
                    sign: this.sign,
                    scope: 'all',
                    timestamp: this.timestamp,
                    id: uuid.v1(),
                })
                return body;
            }catch (e){
                throw new Error("打印授权接口错误");
            }
        },
        textPrinter:async (content,origin_id)=>{
            this.timestamp = getTimestamp();
            this.sign = getSign(this.client_id,this.timestamp,this.client_secret)
            try {
                let body = await post('https://open-api.10ss.net/print/index',{
                    client_id: this.client_id,
                    access_token: this.access_token,
                    machine_code:this.machine_code,
                    content:content,
                    origin_id:origin_id,
                    sign: this.sign,
                    timestamp: this.timestamp,
                    id: uuid.v1(),
                })
                return body;
            }catch (e){
                throw new Error("打印授权接口错误");
            }
        }


    }
}
module.exports = init;